package threadlocal_test;

import org.apache.log4j.Logger;
import org.junit.Test;

/**
 * @author Ajie
 * @date 2021/11/24
 * @function ThreadLocal与WeakReference
 *
 * * 内存泄漏:对象不再使用,但是不能被回收
 * key ThreadLocal 弱引用，
 * value object ，强引用！！！
 * 建议 不用时，主动 remove
 */
public class ThreadLocal_WeakReference {
    private static final Logger logger = Logger.getLogger(ThreadLocal_WeakReference.class);
    public static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    
    @Test
    public void testWeakReference(){
        logger.info("testWeakReference: "+Thread.currentThread());
        threadLocal.set("first");

        String s = threadLocal.get();
        logger.info("testWeakReference: get:"+s);
        threadLocal.remove();
        logger.info("testWeakReference: remove");
        logger.info("testWeakReference: get:"+threadLocal.get());

    }
}
